#include <lobi/thread.h>

#include <windows.h>


class thread_imp
{
	// thread object related data
	thread* client;
	void* data;
	
	public:
		thread_imp() { client = 0; }
		~thread_imp() {}
		
		void set_client(thread* client_) { client = client_; }
		
		void set_data(void* data_) { data = data_; }
		void* get_data() { return data; }

		static DWORD WINAPI entrypoint(LPVOID thisptr_data)
		{
			thread_imp* p = (thread_imp*) thisptr_data;
			p->run(p->get_data());

			return 0;
		}

		bool start(void* data_)
		{
			set_data(data_);

			HANDLE h;
			h = ::CreateThread(NULL,0,entrypoint,(LPVOID)this,0,NULL);
			return h != NULL;
		}

		void run(void* data_)
		{
			client->execute(data_);
		}
};

thread::thread()
{
	imp = (thread_imp_base*)new thread_imp();
}

thread::~thread()
{
	delete imp;
}

bool thread::run(void* data)
{
	imp->set_client(this);
	return imp->start(data);
}


